import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Main {//Initiate Main class

    public static void main(String[] args) {//Initiate main
	
	//Game Window
		setWindow window = new setWindow("testScreen123"); //Initiate window with border heading
		window.setSize(800,600); //Dimensions (includes frame) of window
		window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); //Allow exiting
		window.setResizable(false); //Allow resizing
		window.setVisible(true); //Window visibility

	}//End main
}//End Main class

class setWindow extends JFrame {//JFrame class
      
	Container content = getContentPane();	
	
   public setWindow(String borderHead){//Constructor
      setTitle(borderHead);   
      
      DrawPanel gamePanel = new DrawPanel();  // Call upon drawpanel class
      
      JButton button1 = new JButton("???");
      button1.addActionListener(gamePanel);

      JButton button2 = new JButton("Start");
      button2.addActionListener(gamePanel);

      JButton button3 = new JButton("???");
      button3.addActionListener(gamePanel);
	  
      this.addKeyListener(gamePanel);
	  button1.addKeyListener(gamePanel);
	  button2.addKeyListener(gamePanel);
	  button3.addKeyListener(gamePanel);
	  
      JPanel bottom = new JPanel();   // Button control panel
      bottom.setLayout(new GridLayout(1,3));
      bottom.add(button1);
      bottom.add(button2);
      bottom.add(button3);
   
      content.setLayout(new BorderLayout());
      content.add("Center",gamePanel);             
      content.add("South",bottom);
      
   } // end constructor
}//end setWindow

	class DrawPanel extends JPanel implements ActionListener, KeyListener {

		private Block blocks[] = new Block[10]; // holds a list of the shapesList that are displayed
		private Timer myTimer= new Timer( 60, this );
		boolean up=false;
		DrawPanel() {
        // Constructor: set background color to white set up listeners to respond to mouse actions
			setBackground(Color.yellow);
			for (int i = 0; i < blocks.length-1; i++) {
				String imgName="block"+((int)(Math.random()*4)+1)+".jpg";
				int position =100*i;
				blocks[i]= new Block(position, imgName);
			}
			blocks[blocks.length-1]=new Block(300, "player.jpg");
			addKeyListener(this);
		}   
     
		public void paintComponent(Graphics g) {
			super.paintComponent(g) ;
			for (int i = 0; i < blocks.length; i++) {
				blocks[i].draw(g);
			}
		}   

		public void actionPerformed(ActionEvent evt) {
			
			if (evt.getSource()==myTimer){
				for (int i = 0; i < blocks.length; i++) {
					blocks[i].move();
				}
				
				if (up==true)
					blocks[blocks.length-1].moveUp();
				
				blocks[blocks.length-1].gravityWork();
				repaint();      // redraw canvas to show shape in new position
			}	
		
			else if (evt.getActionCommand().equals("New game")){
			
			}

			else if  (evt.getActionCommand().equals("Move")){
				myTimer.start();
			}		 

		}
		public void keyPressed( KeyEvent e ) {
			System.out.println(e.getKeyCode() );
			if (e.getKeyCode()==38)
				up=true;
		}
		public void keyReleased( KeyEvent e ){
 
			if (e.getKeyCode()==38){
				System.out.println("stop up" );
				up=false;
			}
		}
		public void keyTyped( KeyEvent e ){}
   
	}  // end class DrawPanel

	class Block {

		int left, top;      // Position of top left corner of rectangle that bounds this shape.
		Image im;  
 
		public Block(int pos, String imgName){
			im = new ImageIcon(imgName).getImage();
			left=pos;
			top=350-im.getHeight(null);
		}	
		
		public void draw(Graphics g){
			g.drawImage (im, left, top, null);
		}	
     
		public void move() {
			left -= 5;
        }
		public void moveUp(){
			top-=15;
		}
		public void moveRight(){
			left+=5;
		}
		public void gravityWork(){
			if (top<350-im.getHeight(null))
				top+=2;
		} 	
	}